//
//  TF_LivingVoteCell.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/2/21.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TF_LivingVoteCell: UITableViewCell {

    @objc public var cellWidth:CGFloat = 0
    @objc public var view = UIView()
    @objc public var titleLabel = UILabel()
    @objc public var votingStatus = UILabel()
    @objc public var maskPath: UIBezierPath!
    @objc public var maskLayer = CAShapeLayer.init()
    @objc public var imageUrlView = UIImageView()
    @objc public var pictureVoting = UILabel()
    @objc public var model:TFVoteData?{
        didSet {
            if ((model) != nil)
            {

                self.titleLabel.text = model!.info.label
                self.titleLabel.textColor = UIColor(red: 20/255.0, green: 23/255.0, blue: 26/255.0, alpha: 1.0)
                self.titleLabel.font = UIFont.PingFang(size: 16)
                
                let viewWidth:CGFloat =  self.cellWidth - 12 - 12
                var votingStatusWidth:CGFloat  =  50
                //不是直播结束
                if model!.info.endTime != "0" {
                    votingStatusWidth = 60
                }
              
                self.votingStatus.frame = CGRect(x: viewWidth - votingStatusWidth, y: 0, width: votingStatusWidth, height: 26)
              
              let maskeight:CGFloat = 4

                self.maskPath = UIBezierPath.init(roundedRect: self.votingStatus.bounds, byRoundingCorners: [.topRight,.bottomLeft], cornerRadii: CGSize(width: maskeight, height: maskeight))

                self.maskLayer.frame = self.votingStatus.bounds;
                self.maskLayer.path = self.maskPath.cgPath;
                self.votingStatus.layer.mask = self.maskLayer;

                if (model!.voted) {
                    self.votingStatus.textColor = UIColor(red: 12/255.0, green: 187/255.0, blue: 64/255.0, alpha: 1.0)
                    
                    self.votingStatus.text = "已投票";
                    self.votingStatus.backgroundColor = UIColor(red: 231/255.0, green: 246/255.0, blue: 240/255.0 , alpha: 1.0)
                    
                }//有结束时间
                else   if( model!.info.endTime != "0" ) {
                   
                    self.votingStatus.textColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0)
                    
                    
                    self.votingStatus.backgroundColor = UIColor(red: 225/255.0, green: 240/255.0, blue: 255/255.0, alpha: 1.0)
                    self.votingStatus.text = "投票结束";
                   
                }
               
                else{
                   
                    self.votingStatus.textColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0)
                   
                    self.votingStatus.backgroundColor = UIColor(red: 225/255.0, green: 240/255.0, blue: 255/255.0, alpha: 1.0)
                    self.votingStatus.text = "未投票";
                   
                }
              
                let imageUrl = model!.info.imageUrl as NSString
                //图片模式的投票
                if (imageUrl.length > 0) {
                    self.titleLabel.isHidden = true
                    let url:URL? = URL.init(string: model!.info.imageUrl as String)
                    self.imageUrlView.sd_setImage(with: url, completed: nil)
                
                    self.imageUrlView.isHidden = false
                    self.pictureVoting.isHidden = false
                }else{
                    self.imageUrlView.isHidden = true
                    self.pictureVoting.isHidden = true
                    self.titleLabel.isHidden = false
                }
                
            }
        
        }

    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
     
        self.backgroundColor = UIColor.clear

        self.view.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)

        self.contentView.addSubview(self.view)
        
        self.votingStatus.text = "未投票";
        self.votingStatus.backgroundColor = UIColor(red: 255/255.0, green: 240/255.0, blue: 255/255.0, alpha: 1.0)
        self.votingStatus.layer.cornerRadius = 4;
        self.view.addSubview(self.votingStatus)
        self.votingStatus.textAlignment = .center;
        self.votingStatus.textColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0)
        self.votingStatus.font = UIFont.PingFang(size: 13)
        self.titleLabel.frame = CGRect(x: 24, y: 561, width: 272, height: 44)
        
        self.titleLabel.numberOfLines = 2
        self.view.addSubview(self.titleLabel)
       
        self.imageUrlView.backgroundColor = UIColor(red: 235/255.0, green: 239/255.0, blue: 243/255.0, alpha: 1.0)
        self.view.addSubview(self.imageUrlView)
        self.imageUrlView.contentMode = .scaleAspectFit

        self.pictureVoting.numberOfLines = 0
        self.view.addSubview(self.pictureVoting)

        self.pictureVoting.text = "[图片投票]"
        self.pictureVoting.textColor = UIColor(red: 47/255.0, green: 49/255.0, blue: 52/255.0, alpha: 1.0)
        self.pictureVoting.font = UIFont.PingFang(size: 16)

    }
    override func layoutSubviews() {
        super.layoutSubviews()
      
        self.view.layer.cornerRadius = 4;

        self.view.frame = CGRect(x: 12, y: 0, width: self.frame.size.width - 12 - 12, height: self.frame.size.height - 10)

        
        self.titleLabel.frame = CGRect(x: 12, y: 0, width: self.view.frame.size.width - 67 - 12, height: self.view.frame.size.height)
        
        self.imageUrlView.frame = CGRect(x: 11, y: (self.view.frame.size.height - 20)/2, width: 27, height: 20)
        self.imageUrlView.layer.cornerRadius = 2
        
        self.pictureVoting.frame = CGRect(x: 48, y: (self.view.frame.size.height - 22)/2, width: 75, height: 22)
        
    
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

}
